﻿var VConfStatusValidationClientSideHide = false;
var PageNumber = 1;
var IsSearchView = false;
var VConfStatusIDView = 0;
var IsDeleteView = false;
var VConfStatusComElem = null;
var VConfStatusComElemVal = null;
var VConfStatusTimerId = null;
$(document).ready(function () {
    var path = window.location.pathname;
    if (path.indexOf("SetupVConfStatus") != -1) {
        $.validator.addMethod("SetupVConfStatusRequireds", $.validator.methods.required, Messages["SetupVConfStatusRequireds"]);
        $.validator.addMethod("SetupVConfStatusMaxLength50", $.validator.methods.maxlength, Messages["SetupVConfStatusMaxLength50"]);
        $.validator.addMethod("SetupVConfStatusMaxLength20", $.validator.methods.maxlength, Messages["SetupVConfStatusMaxLength20"]);
        $.validator.addClassRules({
            Required: {
                SetupVConfStatusRequireds: true,
            },
            MaxLength50: {
                SetupVConfStatusMaxLength50: 50,
            },
            MaxLength20: {
                SetupVConfStatusMaxLength20: 20,
            },
        });

        $('#LoadVConfStatusDetailsForm').validate();
        LoadVConfStatusGrid();
    }
});
$(document).on('change', '.ChangedVConfStatusElements', function () {
    $('#VConfStatusDetailsSaveBtn').addClass('BackColorRed');
});
$(document).on('change', '.VConfStatusAddElementChange', function () {
    $('#VConfStatusAddBtn').addClass('BackColorRed');
});
function DeleteVConfStatus(VConfStatusID) {
    jConfirm('Are you sure you want to delete this Vendor Confirm Status?', 'Warning!', function (retVal) {
        if (retVal == true) {
            VConfStatusIDView = VConfStatusID;
            IsDeleteView = true;
            LoadVConfStatusGrid();
        }
    });
}
function AddNewVConfStatus() {
    VConfStatusValidationClientSideHide = false;
    $.ajax({
        url: urls["SetupAddNewVConfStatus"],
        type: 'GET',
        success: function (data) {
            $("#AddNewVConfStatusDiv").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
            IsDeleteView = false;
            AddNewVConfStatusPopUp("AddNewVConfStatusDiv");
        }
    });
}
$(document).on('click', '#VConfStatusMoreBtn', function () {
    PageNumber = PageNumber + 1;
    IsDeleteView = false;
    LoadVConfStatusGrid();
});
$(document).on('click', '#VConfStatusSearchBtn', function () {
    IsSearchView = true;
    IsDeleteView = false;
    LoadVConfStatusGrid();
});
function AddNewVConfStatusPopUp(PopUpDiv) {
    $.colorbox({
        width: "600px",
        height: "245px",
        inline: true,
        href: "#" + PopUpDiv,
        onComplete: function () {
            $("#" + PopUpDiv).show();
        },
        onClosed: function () {
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
            $("#" + PopUpDiv).hide();
        }
    });
}
$(document).on('click', '#VConfStatusAddBtn', function () {
    var $form = $('#AddNewVConfStatusForm');
    if ($form.valid()) {
        VConfStatusValidationClientSideHide = false;
        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            data: $form.serialize(),
            beforeSend: function (xhr) {
                xhr.overrideMimeType('text/plain; charset=x-user-defined');
            },
            success: function (data) {
                if (data.indexOf("Error") != -1)
                    jAlert(data, "Error");
            },
            error: function (xhr) {
                jAlert(xhr.statusText, "Error");
            },
            complete: function () {
                $('#VConfStatusAddBtn').removeClass('BackColorRed')
                jAlert("Data SuccessFully Saved:", "Successful");
                $('#cboxClose').click();
                IsDeleteView = false;
                LoadVConfStatusGrid();
            }
        });
    }
    else
        VConfStatusValidationClientSideHide = true;
});

$(document).on('click', '#VConfStatusDetailsSaveBtn', function () {
    var $form = $('#LoadVConfStatusDetailsForm');
    if ($form.valid()) {
        if ($form[0].length > 0) {
            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: $form.serialize(),
                beforeSend: function (xhr) {
                    xhr.overrideMimeType('text/plain; charset=x-user-defined');
                },
                success: function (data) {
                    if (data.indexOf("Error") != -1)
                        jAlert(data, "Error");
                },
                error: function (xhr) {
                    jAlert(xhr.statusText, "Error");
                },
                complete: function () {
                    $('#VConfStatusDetailsSaveBtn').removeClass('BackColorRed')
                    jAlert("Data SuccessFully Saved:", "Successful");
                    IsDeleteView = false;
                    LoadVConfStatusGrid();
                }
            });
        }
        else
            jAlert("You have not data to save:", "Attention!");
    }
});
function LoadVConfStatusGrid() {
    var SetupVConfStatusGridViewModel = {
        StatusDescText: $("#StatusDescText").val(),
        PageNum: parseInt(PageNumber),
        IsSearch: IsSearchView,
        IsDelete: IsDeleteView,
        StatusID: parseInt(VConfStatusIDView),
        IsActiveSearch: $("#IsActiveSearch").val(),
    };

    var DTO = JSON.stringify(SetupVConfStatusGridViewModel);
    $.ajax({
        url: urls["SetupLoadVConfStatusGrid"],
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: DTO,
        success: function (data) {
            $("#SetupVConfStatusGrid").html(data);
        },
        error: function (xhr) {
            jAlert(xhr.statusText, "Error");
        },
        complete: function () {
            $(".number").maskMoney({ prefix: '', allowNegative: false, thousands: '', decimal: '', affixesStay: false, precision: 0 });
        }
    });
}
// text element complete text 
$(document).on('input', '.QuotaTxtComplete', function () {
    if (VConfStatusTimerId == null) {
        VConfStatusComElem = $(this).attr('id');
        VConfStatusComElemVal = $("#" + VConfStatusComElem).val();
        VConfStatusTimerId = setInterval(function () {

            var keyword = $("#" + VConfStatusComElem).val();
            if (keyword == VConfStatusComElemVal) {
                var PropName = "";
                if (VConfStatusComElem.indexOf("StatusDescText") != -1)
                    PropName = "StatusDescText";

                var position = $("#" + VConfStatusComElem).offset();
                position.top = position.top + $("#" + VConfStatusComElem).height() + 5;
                $.ajax({
                    url: urls["SetupAutoComplete"],
                    data: { 'PropName': PropName, 'keyword': keyword },
                    type: 'POST',
                    success: function (data) {
                        $("#AutoCompleteDiv").html(data);
                        var width = $("#" + VConfStatusComElem).css("width");
                        $("#AutoCompleteDiv").css("width", width);
                        $("#AutoCompleteDiv").show();
                        $("#AutoCompleteDiv").offset(position);
                    },
                    error: function (xhr) {
                        jAlert(xhr.statusText, "Error");
                    },
                    complete: function () {
                        clearInterval(VConfStatusTimerId);
                        VConfStatusTimerId = null;
                    }
                });
            }
            else
                VConfStatusComElemVal = keyword;
        }, 600);
    }
});
$(document).on('click', '.QuotaAutoCompleteDivClose', function () {
    $(this).parent().parent().hide();
});
function htmlDecode(value) {
    return $('<div/>').html(value).text();
}
function AutoCompleteRowClick(key, val) {
    val = htmlDecode(val);
    $("#" + VConfStatusComElem).val(val);
    if (VConfStatusComElem.indexOf("VConfStatusDescText") != -1)
        $("#" + VConfStatusComElem).next().val(key);
    $("#AutoCompleteDiv").hide();
}


var originalClose = $.colorbox.close;
$.colorbox.close = function () {
    if ($('#VConfStatusAddBtn').hasClass('BackColorRed')) {
        jConfirm(Messages["ClosePopUp"], Messages["ClosePopUpTitle"], function (retVal) {
            if (retVal == true) {
                $("#AddNewVConfStatusForm").empty();
                LoadVConfStatusGrid();
                originalClose();
            }
        });
    }
    else {
        $("#AddNewVConfStatusForm").empty();
        LoadVConfStatusGrid();
        originalClose();
    }
};
$(document).on('change', '.ElementsForValidation', function () {
    if ($(this).valid()) {
        $(this).closest("form").find("#errorMessageClient").text("");
    }
    else
        $(this).closest("form").find("#errorMessageServer").hide();
    if (VConfStatusValidationClientSideHide) {
        if ($(this).closest("form").valid()) {
            $(this).closest("form").find("#errorMessageClient").text("");
        }
    }
    else if ($(this).valid()) {
        var elements = "#" + $(this).closest("form").attr("id") + " .error";
        $(elements).not('label').each(function () {
            $(this).valid();
        });
    }
});
$(document).on('change', '.ForValidation', function () {
    if ($(this).valid)
        $(this).removeClass('table_inputs_Unvalidated');
    if ($(this).closest("form").valid()) {
        $(this).closest("form").find("#errorMessageClient").text("");
        var elements = "#" + $(this).closest("form").attr("id") + " .error";
        $(elements).not('label').each(function () {
            if ($(this).valid())
                $(this).removeClass('table_inputs_Unvalidated');
        });
    }
    else
        $(this).closest("form").find("#errorMessageServer").hide();
});